﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TanmiaGrp.GeneralLedger.Core;
using TanmiaGrp.GeneralLedger.Domain;

namespace TanmiaGrp.GeneralLedger.WinApp.Pages
{
    /// <summary>
    /// Interaction logic for ChartOfAccounts.xaml
    /// </summary>
    public partial class ChartOfAccounts : UserControl
    {
        private static AccountDomain bizDomain;
        private Account root;

        public static readonly DependencyProperty SelectedAccountProperty = DependencyProperty.Register("SelectedAccount", typeof(Account), typeof(ChartOfAccounts), new FrameworkPropertyMetadata(OnSelectedAccountChanged));

        private static void OnSelectedAccountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ChartOfAccounts that = d as ChartOfAccounts;
            Account selectedAccount = e.NewValue as Account;
        }

        public Account SelectedAccount
        {
            get { return GetValue(SelectedAccountProperty) as Account; }
            set { SetValue(SelectedAccountProperty, value); }
        }

        public ChartOfAccounts()
        {
            InitializeComponent();

            if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
            {
                bizDomain = new AccountDomain();

                List<Account> parentAccounts = bizDomain.GetChartOfAccounts(null);

                root = Account.ChartOfAccountHeader.Clone() as Account;
                root.AccountChildren = parentAccounts;
                tvAccounts.ItemsSource = new ObservableCollection<Account>(new Account[] { root });
            }
        }

        private void OnSearchTextChanged(object sender, TextChangedEventArgs e)
        {
            Account found = FindAccount(root, txtSearchText.Text);
            if (found != null)
            {
                root.IsExpanded = true;

                found.IsSelected = true;
                Account pointer = found.Parent;
                while (pointer != null)
                {
                    pointer.IsExpanded = true;
                    pointer = pointer.Parent;
                }
            }
        }

        private Account FindAccount(Account root, string s)
        {
            if (root.AccountName.Contains(s) || root.AccountNumber.StartsWith(s))
                return root;

            Account retVal = null;
            foreach (Account acc in root.AccountChildren)
            {
                retVal = FindAccount(acc, s);
                if (retVal != null)
                    return retVal;
            }

            return null;
        }

        private void OnSelectedAccountChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            SelectedAccount = e.NewValue as Account;
        }
    }
}
